<?php
class Product extends Controller {
	function Product() {
		parent::Controller();
		$this->load->helper("url");
		$this->data["base"] = $this->config->item('base_url');
		$this->load->model('Productmodel','',TRUE);
		$this->load->model('TransLog');
		$this->load->database();	
                session_start();
		
	}
	function index() {
		if(((isset($_SESSION['sys_admin'])) and isset($_SESSION['name']))||((isset($_SESSION['admin']))and isset($_SESSION['name']))||((isset($_SESSION['intern_audit']))and isset($_SESSION['name']))||((isset($_SESSION['second_consult']))and isset($_SESSION['name']))||((isset($_SESSION['dept_pic']))and isset($_SESSION['name']))) {
			$this->produc();
		} else {
			redirect('main');
		}
		
	}
	function produc() {
		$max_id = $this->Productmodel->show_product()->row();
		$this->data['default']['prod_id'] = $max_id->prod_id + 1;
		$this->data['h2_title'] = '';
		$this->data['title'] ='Create Product';
		$this->data['form_action'] = site_url('master/product/produc');
		$this->data['link'] = array('link_back' =>anchor('master/product/update_product','kembali',array('class' => 'back')));
		$this->load->vars($this->data);
			$this->load->view('layout/head');
			$this->load->view('layout/body');
			$this->load->view('layout/slice-atas');
			$this->load->view('products/form_input');
			$this->load->view('layout/slice_bawah');
		
		
		
		// pengambilan array
		if(isset($_POST['submit'])) {
				$this->form_validation->set_rules('prod_id','Product Id','required');
				$this->form_validation->set_rules('product','Product','required|callback_valid_product');
				$this->form_validation->set_rules('name','Name','required|callback_valid_name');
				
				//set sesion
				$this->data['product'] = $this->input->post('product');
				$this->data['name'] = $this->input->post('name');
				$this->data['descr'] = $this->input->post('descr');
				$this->data['detail_1'] = $this->input->post('detail_1');
				$this->data['detail_2'] = $this->input->post('detail_2');
				$this->data['detail_3'] = $this->input->post('detail_3');
				$this->data['detail_4'] = $this->input->post('detail_4');
				$this->data['detail_5'] = $this->input->post('detail_5');

				if($this->form_validation->run() == TRUE) {
								$insertProduct = array('prod_id' =>$this->input->post('prod_id'),
										'product' =>strip_tags($this->input->post('product')),
										'name' =>strip_tags($this->input->post('name')),
										'descr' =>strip_tags($this->input->post('descr')),
										'detail_1'=>strip_tags($this->input->post('detail_1')),
										'detail_2'=>strip_tags($this->input->post('detail_2')),
										'detail_3'=>strip_tags($this->input->post('detail_3')),
										'detail_4'=>strip_tags($this->input->post('detail_4')),
										'detail_5'=>strip_tags($this->input->post('detail_5')));
										
				//insert into db
				$this->Productmodel->insert_product($insertProduct);
				$this->session->set_flashdata('message', 'Create Product Sucsess... !');
				
				redirect('master/product');
				} else {
					$this->session->set_flashdata('message', 'Silahkan Ulangi... !');
				}
				
		
		}
		
		
	}
	
	function update_product() {
		$this->data['title'] = 'Manage Product';
		$this->data['h2_title'] ='MANAGE PRODUCT';
		$this->data['cek'] = $this->Productmodel->select_all_product();
		$ap = $this->Productmodel->select_all_product()->result();
		$tmpl = array('table_open' =>'<table border="0" cellpadding="0" cellspacing="0" width="560">','row_alt_start' =>'<tr class="tb-jd">','row_alt_end' =>'</tr>');
			$this->table->set_template($tmpl);
			
			//set heading untul tabel
			$this->table->set_empty("&nbsp;");
			$this->table->set_heading('No','Product','Name','Desc ','Actions');
			
			//Penomeran baris data
			$i = 0;
			foreach($ap as $manage) {
								
				//penyusunan data baris perbaris
				$adesc = substr($manage->descr,0,200);
				$this->table->add_row(++$i,$manage->product,$manage->name,$adesc ,
				anchor('master/product/update_products/'.$manage->prod_id,'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;',array('class'=>'update')).'&nbsp; '.
				anchor('master/product/delete_product/'.$manage->prod_id,'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;',array('class'=>'delete','onclick'=>"return confirm('Anda yakin akan menghapus data ini?')")));
				
			}
			$this->data['table'] = $this->table->generate();
			
			
			$this->data['link'] = array('link_add'=>anchor('master/product/','Create Product',array('class' =>'add')));
			
			$this->load->vars($this->data);
			$this->load->view('layout/head');
			$this->load->view('layout/body');
			$this->load->view('layout/slice-atas');
			$this->load->view('products/update_prod');
			$this->load->view('layout/slice_bawah');	
			
			
	}
	
	function delete_product($prod_id) {
		$this->Productmodel->delete_product($prod_id);
		$this->session->set_flashdata('message', 'Satu data berhasil di hapus ... !');
		
		redirect('master/product/update_product');
		
	}
	
	function update_products($id) {
		$this->data['title'] = 'Update Product';
		$this->data['h2_title'] = '';
		$prod = $this->Productmodel->get_idprod($id)->row();
		$this->data['form_action'] = site_url('master/product/update_process');
		$this->session->set_userdata('pro_id', $prod->prod_id);
		
		$this->data['default']['prod_id'] = $prod->prod_id;
		$this->data['default']['product'] =$prod->product;
		$this->data['default']['name'] = $prod->name;
		$this->data['default']['descr'] = $prod->descr;
		$this->data['default']['detail_1']=$prod->detail_1;
		$this->data['default']['detail_2']=$prod->detail_2;
		$this->data['default']['detail_3']=$prod->detail_3;
		$this->data['default']['detail_4']=$prod->detail_4;
		$this->data['default']['detail_5']=$prod->detail_5;
		
		$this->data['link'] = array('link_back' =>anchor('master/product/update_product','kembali',array('class' => 'back')));
			
			$this->load->vars($this->data);
			$this->load->view('layout/head');
			$this->load->view('layout/body');
			$this->load->view('layout/slice-atas');
			$this->load->view('products/form_input');
			$this->load->view('layout/slice_bawah');	

	}
	function update_process() {
		$this->data['h2_title'] = 'UPDATE PRODUCT';
		//$prod = $this->Productmodel->get_idprod($id)->row();
		$this->data['form_action'] = site_url('master/product/update_process');
		$prod_id = $this->input->post('prod_id');
		
		$table = array('prod_id'=>$this->input->post('prod_id'),
						'product'=>strip_tags($this->input->post('product')),
						'name'=>strip_tags($this->input->post('name')),
						'descr'=>strip_tags($this->input->post('descr')),
						'detail_1'=>strip_tags($this->input->post('detail_1')),
						'detail_2'=>strip_Tags($this->input->post('detail_2')),
						'detail_3'=>strip_tags($this->input->post('detail_3')),
						'detail_4'=>strip_tags($this->input->post('detail_4')),
						'detail_5'=>strip_tags($this->input->post('detail_5')));
						
		//insert data to db
		$this->Productmodel->update_product($prod_id,$table);
		
		$this->session->set_flashdata('message', 'Update Product Sucsess... !');
		
		redirect('master/product/update_product');

		
	}
	function valid_product() {
		if(preg_match("/^[0-9a-zA-z]+$/",$this->input->post('product')))
		{
			return true;
			
			
		} else {
			$this->form_validation->set_message('valid_product', 'Dept Name hanya boleh huruf atau angka ' );
			return false;
		}
		
	}
	function valid_name() {
		if(preg_match("/^[0-9a-zA-z]+$/",$this->input->post('name')))
		{
			return true;
			
			
		} else {
			$this->form_validation->set_message('valid_name', 'Product Name hanya boleh huruf atau angka ' );
			return false;
		}
		
	}
}
?>